package com.aerospace.example.hello.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @ClassName HelloServerServiceImpl
 * @Description
 * @Author yuhang11
 * @Date 2021/12/7 7:06 下午
 * @Version 1.0
 */
@Service
@Slf4j
public class HelloServerServiceImpl extends GreeterGrpc.GreeterImplBase implements HelloServerService, GrpcService {

    public static HelloServerServiceImpl getInstance(){
        return new HelloServerServiceImpl();
    };

    @Override
    public void simpleHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello" + request.getName()).build();
        responseObserver.onNext(reply);
        log.info("-----------simpleHello"+reply.getMessage());
        responseObserver.onCompleted();
    }

    @Override
    public void serverStreamHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        responseObserver.onNext(HelloReply.newBuilder().setMessage("hello1,"+request.getName()).build());
        responseObserver.onNext(HelloReply.newBuilder().setMessage("hello2,"+request.getName()).build());
        responseObserver.onNext(HelloReply.newBuilder().setMessage("hello3,"+request.getName()).build());
        log.info("-----------serverStreamHello");
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<HelloRequest> clientStreamHello(final StreamObserver<HelloReply> responseObserver) {
        return new StreamObserver<HelloRequest>() {
            private HelloReply.Builder builder = HelloReply.newBuilder();
            @Override
            public void onNext(HelloRequest helloRequest) {
                builder.setMessage(builder.getMessage()+","+helloRequest.getName());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                builder.setMessage("hello"+builder.getMessage());
                log.info("-----------clientStreamHello"+builder.getMessage());
                responseObserver.onNext(builder.build());

                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<HelloRequest> biStreamHello(final StreamObserver<HelloReply> responseObserver) {
        return new StreamObserver<HelloRequest>() {
            @Override
            public void onNext(HelloRequest helloRequest) {
                responseObserver.onNext(HelloReply.newBuilder().setMessage("hello1,"+helloRequest.getName()).build());
                responseObserver.onNext(HelloReply.newBuilder().setMessage("hello2,"+helloRequest.getName()).build());
                responseObserver.onNext(HelloReply.newBuilder().setMessage("hello3,"+helloRequest.getName()).build());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                log.info("---------biStreamHello");
                responseObserver.onCompleted();
            }
        };
    }
}
